package com.lekang.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lekang.common.annotation.Log;
import com.lekang.common.core.controller.BaseController;
import com.lekang.common.core.domain.AjaxResult;
import com.lekang.common.enums.BusinessType;
import com.lekang.system.domain.TodoTask;
import com.lekang.system.service.ITodoTaskService;
import com.lekang.common.utils.poi.ExcelUtil;
import com.lekang.common.core.page.TableDataInfo;
import java.util.stream.Collectors;

/**
 * 待办单据（存储入住、退住、请假等待办事项）Controller
 * 
 * @author zhihao
 * @date 2025-10-16
 */
@RestController
@RequestMapping("/system/task")
public class TodoTaskController extends BaseController
{
    @Autowired
    private ITodoTaskService todoTaskService;

    /**
     * 查询待办单据（存储入住、退住、请假等待办事项）列表
     */
    // @PreAuthorize("@ss.hasPermi('system:task:list')")  // 临时注释权限检查
    @GetMapping("/list")
    public TableDataInfo list(TodoTask todoTask)
    {
        startPage();
        List<TodoTask> list = todoTaskService.selectTodoTaskList(todoTask);
        
        String currentUsername = getUsername();
        System.out.println("=== 我的申请页面权限检查 ===");
        System.out.println("当前用户: " + currentUsername);
        System.out.println("申请总数: " + (list != null ? list.size() : 0));
        
        // 精确权限控制：根据用户角色和申请类型进行过滤
        list = list.stream()
            .filter(task -> {
                String todoType = task.getTodoType();
                
                // 退住申请：只有退住审批员a能看
                if ("退住".equals(todoType)) {
                    boolean canSee = "tuizhushenpiyuana".equals(currentUsername);
                    System.out.println("退住申请：用户=" + currentUsername + ", 可见=" + canSee);
                    return canSee;
                }
                
                // 请假申请：只有护理组组长能看
                if ("请假".equals(todoType)) {
                    boolean canSee = "hulizuzuzhang".equals(currentUsername);
                    System.out.println("请假申请：用户=" + currentUsername + ", 可见=" + canSee);
                    return canSee;
                }
                
                // 其他类型：所有用户都能看
                return true;
            })
            .collect(Collectors.toList());
        
        System.out.println("过滤后的申请数量: " + list.size());
        return getDataTable(list);
    }

    /**
     * 导出待办单据（存储入住、退住、请假等待办事项）列表
     */
    @PreAuthorize("@ss.hasPermi('system:task:export')")
    @Log(title = "待办单据（存储入住、退住、请假等待办事项）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TodoTask todoTask)
    {
        List<TodoTask> list = todoTaskService.selectTodoTaskList(todoTask);
        ExcelUtil<TodoTask> util = new ExcelUtil<TodoTask>(TodoTask.class);
        util.exportExcel(response, list, "待办单据（存储入住、退住、请假等待办事项）数据");
    }

    /**
     * 获取待办单据（存储入住、退住、请假等待办事项）详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(todoTaskService.selectTodoTaskById(id));
    }

    /**
     * 新增待办单据（存储入住、退住、请假等待办事项）
     */
    @PreAuthorize("@ss.hasPermi('system:task:add')")
    @Log(title = "待办单据（存储入住、退住、请假等待办事项）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TodoTask todoTask)
    {
        return toAjax(todoTaskService.insertTodoTask(todoTask));
    }

    /**
     * 修改待办单据（存储入住、退住、请假等待办事项）
     */
    @PreAuthorize("@ss.hasPermi('system:task:edit')")
    @Log(title = "待办单据（存储入住、退住、请假等待办事项）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TodoTask todoTask)
    {
        return toAjax(todoTaskService.updateTodoTask(todoTask));
    }

    /**
     * 删除待办单据（存储入住、退住、请假等待办事项）
     */
    @PreAuthorize("@ss.hasPermi('system:task:remove')")
    @Log(title = "待办单据（存储入住、退住、请假等待办事项）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(todoTaskService.deleteTodoTaskByIds(ids));
    }
}
